
package com.grablist.feihu.grablist.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import com.grablist.feihu.grablist.R;
import com.grablist.feihu.grablist.bean.CityInfo;
import com.grablist.feihu.grablist.event.CityEvent;

/**
 * Created by super南仔 on 2017-05-09. 类备注： 需要传入的参数:
 */
public class CityAdapter extends RecyclerView.Adapter<CityAdapter.ViewHolder> {
    protected Context mContext;
    protected List<CityInfo> mDatas;
    protected LayoutInflater mInflater;

    public CityAdapter(Context mContext, List<CityInfo> mDatas) {
        this.mContext = mContext;
        this.mDatas = mDatas;
        mInflater = LayoutInflater.from(mContext);
    }

    public List<CityInfo> getDatas() {
        return mDatas;
    }

    public CityAdapter setDatas(List<CityInfo> datas) {
        mDatas = datas;
        return this;
    }

    @Override
    public CityAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mInflater.inflate(R.layout.view_city_layout, parent, false));
    }

    @Override
    public void onBindViewHolder(final CityAdapter.ViewHolder holder, final int position) {
        holder.tvCity.setText(mDatas.get(position).getName());
        holder.tvCity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EventBus.getDefault().post(new CityEvent(mDatas.get(position).getName()));
            }
        });

    }

    @Override
    public int getItemCount() {
        return mDatas != null ? mDatas.size() : 0;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvCity;

        public ViewHolder(View itemView) {
            super(itemView);
            tvCity = (TextView) itemView.findViewById(R.id.super_tv_name);
        }
    }
}
